#!/bin/bash

# SSL Read 监听器构建脚本

set -e

echo "=== SSL Read 监听器构建脚本 ==="

# 检查依赖
echo "检查依赖..."

# 检查 Go
if ! command -v go &> /dev/null; then
    echo "错误: 未找到 Go 编译器"
    exit 1
fi

# 检查 Clang
if ! command -v clang &> /dev/null; then
    echo "错误: 未找到 Clang 编译器"
    echo "请安装: sudo apt install clang llvm libelf-dev"
    exit 1
fi

# 检查权限
if [ "$EUID" -eq 0 ]; then
    echo "警告: 不建议以 root 权限运行构建脚本"
fi

echo "依赖检查完成"

# 安装 Go 依赖
echo "安装 Go 依赖..."
go mod tidy

# 测试依赖
echo "测试依赖..."
go run test_deps.go

# 编译 eBPF 程序
echo "编译 eBPF 程序..."
clang -O2 -g -Wall -target bpf -c ssl_ebpf.c -o ssl_ebpf.o

# 生成 Go 绑定
echo "生成 Go 绑定..."
go generate

# 编译主程序
echo "编译主程序..."
go build -tags ebpf -o ssl_monitor main.go

# 编译测试程序
echo "编译测试程序..."
go build -o test_ssl test_ssl.go

echo "=== 构建完成 ==="
echo "可执行文件:"
echo "  - ssl_monitor (主程序)"
echo "  - test_ssl (测试程序)"
echo ""
echo "运行主程序:"
echo "  sudo ./ssl_monitor"
echo ""
echo "运行测试程序:"
echo "  ./test_ssl" 